<#include "../common/common.html"/> <@layout>
<script src="${ctx}/static/inc/platform/js/ajax.js"></script>

<script type="text/javascript">
    var url,ids = [];
    /**
     * 打开新增用户的表单，录入用户信息
     */
    function neworgModel() {
        $('#org').dialog('setTitle', '新增机构').dialog('open');
        $('#orgform').form('clear');
        $('#pid').combotree({
            url:"/common/comboTree?table=auth_org&fields=id as id,name as text&pid=pid&pidStart=0&id=id"
        });
        $('#pid').combotree('setValue','${orgid}');
        url = '/curd/save/auth_org';
    }
    /**
     * 编辑选中的用户信息
     */
    function editorgModel() {
        $('#orgform').form('clear');
//        send_request_update('${ctx}/authOrg/edit?id='+'${orgid}' ,'',saveorg);
        send_request('${ctx}/authOrg/edit?id='+'${orgid}','saveorg',getHTML);
    }


    function getHTML(){
        if (http_request.readyState == 4) { // 判断对象状态
            var ajaxobj=document.getElementById("saveorg");
            if (http_request.status == 200) { // 信息已经成功返回，开始处理信息
                ajaxobj.innerHTML = unescape(http_request.responseText);
                //重新加载js

                $('#pid').combotree({
                    url:"/common/comboTree?table=auth_org&fields=id as id,name as text&pid=pid&pidStart=0&id=id"
                });
                $('#pid').combotree('setValue','${pid}');
                url = '/curd/update/auth_org';
                $('#org').dialog('setTitle', '修改机构').dialog('open');
                //f_load();
                querying--;
            } else { //页面不正常
                ajaxobj.innerHTML="您所请求的页面有异常。";
            }
        }
    }
    /**
     * 保存新增的用户数据
     */
    function saveorgModel() {
        var id=document.orgform.id.value;
        var url="";
             if(id !=""){
                 url="/curd/update/auth_org" ;
            } else{
                     url="/curd/save/auth_org" ;
            }

        $('#orgform').form('submit', {
        url :url,
            onSubmit : function() {
                return $(this).form('validate');
            },
            success : function(data) {
                $.messager.show({ // show error message
                    title : '提示',
                    msg : data,
                    timeout:2000
                });
                $('#org').dialog('close');
                var selectNode = parent.treeObj.getSelectedNodes();
//                alert(selectNode[0]);
//                if(selectNode[0].isParent) {  //若该节点有下级，则该节点刷新
                    parent.treeObj.reAsyncChildNodes(null, "refresh");    //从根节点刷新
//                } else {   //该节点无下级，则让上级节点刷新
//                    parent.treeObj.updateNode(selectNode[0]); //让上一行更新生效
//                    parent.treeObj.reAsyncChildNodes(selectNode[0].getParentNode(), "refresh");//父节点刷新
//                }
            }
        });
    }

    function deleteorgModel() {
            $.messager.confirm('确认', '您确定要删除选中的模型吗?', function(r) {
                if (r) {
                    $.post('/curd/delete/auth_org-' + '${orgid}', function(data) {
                        $.messager.show({
                            title : '提示',
                            msg : data,
                            timeout:2000
                        });
                        var selectNode = parent.treeObj.getSelectedNodes();
                        parent.treeObj.reAsyncChildNodes(null, "refresh");  //根节点刷新
                    },'text');
                }
            });

    }


    /**
     * 打开新增用户的表单，录入用户信息
     */
    function newModel() {
        $('#dlg').dialog('setTitle', '新增模型').dialog('open');
        $('#myform').form('clear');
        reloadCombtree();
        $('#level').val("3");
        $('#orgid').combotree('setValue','${orgid}');
        $('#roles').tree({
            url:'${ctx}/authUser/rolesTree',
            checkbox:true
        });
        $('#perms').tree({
            url:'${ctx}/authRole/permsTree',
            checkbox:true
        });
        url = '${ctx}/authUser/save';
    }
    /**
     * 编辑选中的用户信息
     */
    function editModel() {
        $('#myform').form('clear');
        var row = $('#dt-grids').datagrid('getSelected');
        if (row) {
            $('#dlg').dialog('setTitle', '修改用户').dialog('open');
            reloadCombtree();
            $('#myform').form('load', row);
            /*$('#f_zp_temp').attr("src","${ctx}/authUser/download/"+row.id+"?r="+Math.random());
            $('#f_zp_temp').show();*/
            $('#roles').tree({
                url:'${ctx}/authUser/rolesTree/'+row.id,
                checkbox:true
            });
            $('#perms').tree({
                url:'${ctx}/authRole/userpermsTree/'+row.id,
                checkbox:true
            });
            url = '${ctx}/authUser/update';
        } else {
            $.messager.show({
                title : '提示',
                msg : '请先选择模型，再进行修改。',
                timeout:2000
            });
        }
    }

    function deleteModel() {
        var row = $('#dt-grids').datagrid('getSelected');
        if (row) {
            $.messager.confirm('确认', '您确定要删除选中的模型吗?', function(r) {
                if (r) {
                    $.post('${ctx}/authUser/delete/' + row.id, function(data) {
                        $.messager.show({
                            title : '提示',
                            msg : data,
                            timeout:2000
                        });
                        $('#dt-grids').datagrid('reload'); // reload the user data
                    },'text');
                }
            });
        }else {
            $.messager.show({
                title : '提示',
                msg : '请先选择模型，再进行删除。',
                timeout:2000
            });
        }
    }
    /**
     * 保存新增的用户数据，并对登录密码进行校验
     */
    function saveModel() {
        ids = [];
        var nodes = $('#roles').tree("getChecked");
        var num = nodes.length;
        for ( var i = 0; i < num; i++) {
            ids.push(nodes[i].id+"");
        }
        $('#roleIds').val(ids.join(','));
        var perms = $('#perms').tree("getChecked");
        ids = [];//清空值
        var num = perms.length;
        if (num < 1) {
            alert  ("您没有给该用户选择任何权限");
            return ;
        } else {
            for ( var i = 0; i < num; i++) {
                $.getNodes($('#perms'),perms[i]);
            }
        }
        $('#permIds').val(ids.join(','));
        $('#myform').form('submit', {
            url : url,
            onSubmit : function() {
                return $(this).form('validate');
            },
            success : function(data) {
                $.messager.show({ // show error message
                    title : '提示',
                    msg : data,
                    timeout:2000
                });
                $('#dlg').dialog('close');
                $('#dt-grids').datagrid('reload');
            }
        });
    }

    function query() {
        $('#dt-grids').datagrid('load', $.getQueryParam('queryTable'));
        $('#qdlg').dialog('close');
    }

    function queryModel() {
        $('#qdlg').dialog('setTitle', '查询条件').dialog('open');
    }

    function reloadCombtree(){
        <#list fdsList as m>
        $('#${m.id}').combotree('reload').combotree('setValue',0);
        </#list>
    }

    $(function(){
        <#list fdsList as m>
        $('#${m.id}').combotree({
            url:"${ctx}/${m.url}"
        });
        </#list>
        <#list qFdsList as m>
        $('#q_${m.id}').combotree({
            url:"${ctx}/${m.url}"
        });
        </#list>
        $('#dt-grids').datagrid({
            columns:[[
            <#list list as m>
                {field:'${m.fieldname}',title:'${m.fielddesc}',width:100<#if m.data??>,data:${m.data},formatter: function(value){
                    var data = this.data;
                    for(var i=0,len=data.length;i<len;i++){
                        if(value == data[i].id){
                            return data[i].text;
                        }
                    }
                }</#if>}<#if m_has_next>,</#if>
            </#list>
            ]]
        });
    });
    /*function uploadZp(){
        $('#form_zp').form('submit', {
            url : '${ctx}/authUser/upload',
            success : function(data) {
                var d = $.parseJSON(data);
                if(d.img){
                    $('#f_zp_temp').attr("src",'${ctx}' + d.img + "?r="+Math.random());
                    $('#f_zp_temp').show();
                    $('#zp_file').val(d.img);
                }else{
                    $.messager.show({ // show error message
                        title : '提示',
                        msg : d.msg,
                        timeout:2000
                    });
                }
            }
        });
    }*/
</script>
<body>
	<table id="dt-grids" class="easyui-datagrid"
		data-options="fit:true,singleSelect:true,rownumbers:true,pagination:true,toolbar:'#toolbar',url:'${ctx}/authUser/query?orgid='+${orgid}">

	</table>

	<div id="toolbar">
        <a href="#" class="easyui-linkbutton"
           data-options="iconCls:'icon-add',plain:true" onclick="neworgModel()">新增机构</a>
        <a href="#" class="easyui-linkbutton"
           data-options="iconCls:'icon-edit',plain:true" onclick="editorgModel()">编辑机构</a>
        <a href="#" class="easyui-linkbutton"
           data-options="iconCls:'icon-remove',plain:true"
           onclick="deleteorgModel()">删除机构</a>
        <a href="#" class="easyui-linkbutton"
			data-options="iconCls:'icon-add',plain:true" onclick="newModel()">新增用户</a>
		<a href="#" class="easyui-linkbutton"
			data-options="iconCls:'icon-edit',plain:true" onclick="editModel()">编辑用户</a>
		<a href="#" class="easyui-linkbutton"
			data-options="iconCls:'icon-remove',plain:true"
			onclick="deleteModel()">删除用户</a> <a href="#" class="easyui-linkbutton"
			data-options="iconCls:'icon-search',plain:true"
			onclick="queryModel()">用户查询</a>
	</div>
	<div id="dlg" class="easyui-dialog"
		style="top:60%;left:60%;width: 600px; height: 450px;"
		data-options="closed:true,modal:true,buttons:'#dlg-buttons'">
		<div id='index_tabs' class="easyui-tabs"
			data-options="fit:true,border:false">
			<div title="用户基本信息">
				<form id="myform" method="post">
					<input type="hidden" name="id" /> <input type="hidden" id="permIds"
						name="permIds" /> <input type="hidden" id="roleIds" name="roleIds" />
					<table cellpadding="0" cellspacing="0" class="form-table"
						align='center'>
						<#list cu as m> <#if m_index%2==0>
						<tr>
							</#if>
							<td style="width: 100px">${m.fielddesc}：</td>
							<td style="width: 180px"><#if m.datatype=="timestamp"><input
								id="${m.fieldname}" name="${m.fieldname}"
								class="easyui-datetimebox"<#if
								m.isnull=="no">data-options="required:true"</#if> type="text"
								style="width: 150px"/> <#elseif m.datatype=="date"><input
								id="${m.fieldname}" name="${m.fieldname}" class="easyui-datebox"<#if
								m.isnull=="no">data-options="required:true"</#if> type="text"
								style="width: 150px"/> <#else><input id="${m.fieldname}"
								name="${m.fieldname}"<#if m.isnull=="no"><#if
								!m.cmb>class="easyui-validatebox"</#if>
								data-options="required:true"</#if> type="text" style="width:
								150px"/></#if>
                            </td><#if m_index%2==1>

						</tr>
						</#if> </#list>
                        <td style="width: 100px">用户类型</td>
                        <td style="width: 180px">
                            <select name="level"
                            id="level" class="easyui-validatebox  query" panelHeight="100"
                            style="width: 150px">
                            <option value="1">系统管理员</option>
                            <option value="2" >部门领导</option>
                            <option value="3" selected="selected" >员工</option>
                            </select>
                        </td>
                        <tr>

                        <td style="width: 100px">有效标志</td>
                        <td style="width: 180px">
                            <select name="yxbz"
                                    id="yxbz" class="easyui-validatebox  " panelHeight="100"
                                    style="width: 150px">
                                <option value="1"  selected="selected">有效</option>
                                <option value="0" >无效</option>
                            </select>
                        </td>
                        </tr>
                    </table>
				</form>
			</div>
			<div title="角色">
				<ul id="roles" class="easyui-tree"></ul>
			</div>
			<div title="权限">
				<ul id="perms" class="easyui-tree"></ul>
			</div>
		</div>
		<div id="dlg-buttons" style="text-align: center">
			<a href="#" class="easyui-linkbutton" iconCls="icon-save"
				onclick="saveModel()">保存</a> <a href="#" class="easyui-linkbutton"
				iconCls="icon-cancel" onclick="javascript:$('#dlg').dialog('close')">取消</a>
		</div>
	</div>
	<div id="qdlg" class="easyui-dialog"
		style="width: 500px; height: 450px; padding: 10px" closed="true"
		modal="true" buttons="#query-buttons">
		<table id='queryTable' cellpadding="0" cellspacing="0"
			class="form-table" align='center'>
			<#list query as m> <#if m_index%2==0>
			<tr>
				</#if>
				<td style="width: 80px">${m.fielddesc}：</td>
				<td style="width: 180px"><input id="q_${m.fieldname}"
					name="search_EQ_${m.fieldname}" class="query" style="width: 130px" /></td><#if
				m_index%2==1>
			</tr>
			</#if> </#list>
		</table>
		<div id="query-buttons" style="text-align: center">
			<a href="#" class="easyui-linkbutton" iconCls="icon-search"
				onclick="query()">查询</a> <a href="#" class="easyui-linkbutton"
				iconCls="icon-cancel"
				onclick="javascript:$('#qdlg').dialog('close')">取消</a>
		</div>
	</div>
    <div id="org" class="easyui-dialog"
         data-options="closed:true,modal:true,buttons:'#org-buttons'"
         style="width: 600px; height: 450px;">
        <form id="orgform" name="orgform" method="post"><#include
                "authOrg_form.html"/></form>
        <div id="org-buttons" style="text-align: center">
            <a href="#" class="easyui-linkbutton"
               data-options="iconCls:'icon-save'" onclick="saveorgModel()">保存</a> <a
                href="#" class="easyui-linkbutton"
                data-options="iconCls:'icon-cancel'"
                onclick="javascript:$('#org').dialog('close')">取消</a>
        </div>
    </div>
</body>
</@layout>
